package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.excitation.bean.user.UserObject;
import com.lishe.lishe.lishe.module.excitation.presenter.ExcitationPresenter;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.StringUtils;
import com.lishe.lishe.retrofit.DataResponseListener;

/**
 * Created by yang on 2017/11/24.
 */

public class PersonModifyNameActivity extends BaseActivity implements View.OnClickListener {

    public static final void jump(Activity activity, UserObject user,boolean updateEmail) {
        Intent intent = new Intent(activity, PersonModifyNameActivity.class);
        intent.putExtra("updateEmail",updateEmail);
        mUserObject = user;
        activity.startActivityForResult(intent, PersonMessageModifyActivity.REQUEST_CODE_MODIFY_NAME);
    }

    private Button mButtonBack,mButtonSure;
    private TextView mTextTitle;
    private EditText mEditName;
    private ImageView mImageDismiss,mImageIcon;

    private static UserObject mUserObject;
    private boolean mUpdateEmail=false;
    private InputMethodManager mInputManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_modify_name);
        mUpdateEmail=getIntent().getBooleanExtra("updateEmail",false);
        initView();
        setListener();
    }

    @Override
    public void onBackPressed() {
        back();
    }

    private void initView() {
        mButtonBack=findViewById(R.id.title_left_button);
        mButtonSure=findViewById(R.id.title_rigth_button);
        mTextTitle=findViewById(R.id.title_text_title);
        mEditName=findViewById(R.id.name);
        mImageDismiss=findViewById(R.id.dismiss);
        mImageIcon=findViewById(R.id.icon);

        if(mUpdateEmail) {
            mEditName.setHint(R.string.please_set_your_email);
            mTextTitle.setText(R.string.email);
            mImageIcon.setImageResource(R.mipmap.mail_icon);
        }else{
            mTextTitle.setText(R.string.name);
        }
        mButtonSure.setVisibility(View.VISIBLE);
        mButtonSure.setText(R.string.sure);
        mButtonSure.setBackground(null);
        mButtonSure.setTextColor(getResources().getColor(R.color.color_999999));

        mInputManager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    private void hideSoftInput(){
        if(mInputManager!=null)
            mInputManager.hideSoftInputFromWindow(mEditName.getWindowToken(),0);
    }

    private void setListener() {
        mButtonBack.setOnClickListener(this);
        mImageDismiss.setOnClickListener(this);
        mButtonSure.setOnClickListener(this);

        mEditName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(s.toString())){
                    mImageDismiss.setVisibility(View.GONE);
                }else{
                    mImageDismiss.setVisibility(View.VISIBLE);
                }
            }
        });

        if(mUpdateEmail){
            mEditName.setText(mUserObject.getEmpEmail());
        }else{
            mEditName.setText(mUserObject.getEmpName());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_left_button:
                back();
                break;
            case R.id.dismiss:
                mEditName.setText("");
                break;
            case R.id.title_rigth_button:
                hideSoftInput();
                String str=mEditName.getText().toString().trim();
                if(TextUtils.isEmpty(str)){
                    toast(getString(R.string.input_content_no_null));
                    return;
                }
                if(!StringUtils.containsEmoji(str)) {
                    if (mUpdateEmail) {
                        if(StringUtils.checkEmaile(str)) {
                            ExcitationPresenter.getInstance().updatePersonEmail(Globals.getToken(), str, listener);
                        }else{
                            toast(getString(R.string.is_not_validity_email_address));
                        }
                    } else {
                        if(str.length()>20){
                            toast(getString(R.string.name_length_no_too_large));
                        }else {
                        ExcitationPresenter.getInstance().updatePersonName(Globals.getToken(), str, listener);
                        }
                    }
                }else{
                    toast(getString(R.string.no_contains_emoji));
                }
                break;
        }
    }

    private void back(){
        hideSoftInput();
        setResult(RESULT_OK,null);
        finish();
    }

    private DataResponseListener<String> listener=new DataResponseListener<String>() {

        @Override
        public void onPrepare() {
            LoadingUtils.createLoadingDialog(PersonModifyNameActivity.this,"请稍候...",false);
        }

        @Override
        public void onSuccess(String str) {
            if(mUpdateEmail) {
                mUserObject.setEmpEmail(str);
            }else{
                mUserObject.setEmpName(str);
            }

            LoadingUtils.closeDialog();
            back();
        }

        @Override
        public void onFail(String failMessage) {
            toast(failMessage);
            LoadingUtils.closeDialog();
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            LoadingUtils.closeDialog();
        }
    };
}
